'
' DotNetNuke -  http://www.dotnetnuke.com
' Copyright (c) 2002-2005
' by Shaun Walker ( sales@perpetualmotion.ca ) of Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports System.Data.SqlClient
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Web
Imports DotNetNuke
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Security
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Tabs

Namespace DotNetNuke.Modules.WhatsNew

    ''' -----------------------------------------------------------------------------
    ''' Namespace:  DotNetNuke.Modules.WhatsNew
    ''' Project:    DotNetNuke.WhatsNew
    ''' Class:      WhatsNew
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The WhatsNew Class provides the UI for displaying recently added or updated
    ''' content.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    '''		[cnurse]	03/17/2005  created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public MustInherit Class WhatsNew
        Inherits Entities.Modules.PortalModuleBase


#Region "Controls"
        Protected WithEvents dgResults As System.Web.UI.WebControls.DataGrid
#End Region

#Region "Private Methods"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' BindData binds the Whats New Items to the Grid
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' 	[cnurse]	3/17/2005	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub BindData()

            Dim objTabs As New TabController
            Dim objTab As TabInfo
            Dim objModules As New ModuleController
            Dim objModule As ModuleInfo
            Dim iNew As Integer = 0

            ' get items
            Dim i As Integer = 0
            Dim ResultItem As SearchResultsInfo
            Dim Results As SearchResultsInfoCollection = SearchDataStoreProvider.Instance.GetSearchItems(PortalId, Null.NullInteger, Null.NullInteger)

            Dim dt As New DataTable
            Dim dc As New DataColumn("TabId")
            dt.Columns.Add(New DataColumn("TabId", GetType(System.Int32)))
            dt.Columns.Add(New DataColumn("Guid", GetType(System.String)))
            dt.Columns.Add(New DataColumn("Title", GetType(System.String)))
            dt.Columns.Add(New DataColumn("Description", GetType(System.String)))
            dt.Columns.Add(New DataColumn("PubDate", GetType(System.DateTime)))

            'Get the no of days to display
            Dim fromDate As Date = Date.MinValue
            If CType(Settings("nodays"), String) <> "" Then
                fromDate = Date.Today().AddDays(-Integer.Parse(CType(Settings("nodays"), String)))
            End If

            'Get the maximum items to display
            Dim maxItems As Integer = 0
            If CType(Settings("maxresults"), String) <> "" Then
                maxItems = Integer.Parse(CType(Settings("maxresults"), String))
            End If
            'Get the minimum items to display
            Dim minItems As Integer = 0
            If CType(Settings("minresults"), String) <> "" Then
                minItems = Integer.Parse(CType(Settings("minresults"), String))
            End If

            'Get the items/page to display
            Dim itemsPage As Integer = 10
            If CType(Settings("perpage"), String) <> "" Then
                itemsPage = Integer.Parse(CType(Settings("perpage"), String))
            End If

            'Get the titlelength/descriptionlength
            Dim titleLength As Integer = 0
            If CType(Settings("titlelength"), String) <> "" Then
                titleLength = Integer.Parse(CType(Settings("titlelength"), String))
            End If
            Dim descLength As Integer = 0
            If CType(Settings("descriptionlength"), String) <> "" Then
                descLength = Integer.Parse(CType(Settings("descriptionlength"), String))
            End If

            'Parse through the Items
            For i = 0 To Results.Count - 1
                ResultItem = Results(i)
                objTab = objTabs.GetTab(ResultItem.TabId)
                If PortalSecurity.IsInRoles(objTab.AuthorizedRoles) And CType(IIf(objTab.StartDate = Null.NullDate, Date.MinValue, objTab.StartDate), Date) < Now And CType(IIf(objTab.EndDate = Null.NullDate, Date.MaxValue, objTab.EndDate), Date) > Now Then
                    objModule = objModules.GetModule(ResultItem.ModuleId, ResultItem.TabId)
                    If objModule.IsDeleted = False And PortalSecurity.IsInRoles(objModule.AuthorizedViewRoles) = True And CType(IIf(objModule.StartDate = Null.NullDate, Date.MinValue, objModule.StartDate), Date) < Now And CType(IIf(objModule.EndDate = Null.NullDate, Date.MaxValue, objModule.EndDate), Date) > Now Then
                        If ResultItem.PubDate > fromDate Or iNew < minItems Then
                            Dim dr As DataRow = dt.NewRow()
                            dr("TabId") = ResultItem.TabId
                            dr("Guid") = ResultItem.Guid
                            If titleLength > 0 And titleLength < ResultItem.Title.Length Then
                                dr("Title") = ResultItem.Title.Substring(0, titleLength)
                            Else
                                dr("Title") = ResultItem.Title
                            End If
                            If descLength > 0 And descLength < ResultItem.Description.Length Then
                                dr("Description") = ResultItem.Description.Substring(0, descLength)
                            Else
                                dr("Description") = ResultItem.Description
                            End If
                            dr("PubDate") = ResultItem.PubDate
                            dt.Rows.Add(dr)
                            iNew += 1
                        Else
                            Exit For
                        End If

                        If iNew >= maxItems Then Exit For

                    End If
                End If
            Next

            'Bind Results Grid
            Dim dv As New DataView(dt)
            dgResults.PageSize = itemsPage
            dgResults.DataSource = dv
            dgResults.DataBind()

            If iNew = 0 Then
                dgResults.Visible = False
            End If
            If iNew <= dgResults.PageSize Then
                dgResults.PagerStyle.Visible = False
            Else
                dgResults.PagerStyle.Visible = True
            End If
        End Sub

#End Region

#Region "Public Methods"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' FormatDate displays the publication Date
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="pubDate">The publication Date</param>
        ''' <returns>The formatted date</returns>
        ''' <history>
        ''' 	[cnurse]	3/17/2005	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function FormatDate(ByVal pubDate As Date) As String

            Return pubDate.ToShortDateString()

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' FormatURL the correctly formatted url to the Search Result
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="TabID">The Id of the Tab where the content is located</param>
        ''' <param name="Link">The module provided querystring to access the correct content</param>
        ''' <returns>The formatted url</returns>
        ''' <history>
        ''' 	[cnurse]	3/17/2005	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function FormatURL(ByVal TabID As Integer, ByVal Link As String) As String

            Dim strURL As String

            If Link = "" Then
                strURL = Common.NavigateURL(TabID)
            Else
                strURL = Common.NavigateURL(TabID, "", Link)
            End If

            Return strURL

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' ShowDescription determines whether the description should be shown
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <returns>True or False string</returns>
        ''' <history>
        ''' 	[cnurse]	3/17/2005	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function ShowDescription() As String

            Dim strShow As String

            If CType(Settings("showdescription"), String) <> "" Then
                If CType(Settings("showdescription"), String) = "Y" Then
                    strShow = "True"
                Else
                    strShow = "False"
                End If
            Else
                strShow = "False"
            End If

            Return strShow

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' ShowDate determines whether the date should be shown
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <returns>True or False string</returns>
        ''' <history>
        ''' 	[cnurse]	3/17/2005	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function ShowDate() As String

            Dim strShow As String

            If CType(Settings("showdate"), String) <> "" Then
                If CType(Settings("showdate"), String) = "Y" Then
                    strShow = "True"
                Else
                    strShow = "False"
                End If
            Else
                strShow = "False"
            End If

            Return strShow

        End Function
#End Region

#Region "Event Handlers"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' 	[cnurse]	11/11/2004	documented
        '''     [cnurse]    12/13/2004  Switched to using a DataGrid for Search Results
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not Page.IsPostBack Then
                BindData()
            End If
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' dgResults_PageIndexChanged runs when one of the Page buttons is clicked
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''     [cnurse]    12/13/2004  created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub dgResults_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgResults.PageIndexChanged
            dgResults.CurrentPageIndex = e.NewPageIndex
            BindData()
        End Sub

#End Region

#Region " Web Form Designer Generated Code "
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
#End Region

    End Class

End Namespace
